using System;
using System.Collections.Generic;

namespace shfa.snomtastic.data.business
{
    public class EventLogComparer : IComparer<EventLogInfo>
    {
        private string _sortExpression;
        public string SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

		private string _sortDirection;
        public string SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }

        public EventLogComparer(string sortExpression)
        {
            int iSplitPos = sortExpression.IndexOf(" ");
            SortExpression = (iSplitPos > 0) ? sortExpression.Substring(0, iSplitPos) : sortExpression;
            SortDirection = (iSplitPos > 0) ? sortExpression.Substring(iSplitPos + 1).ToUpper() : "ASC";
        }

        public EventLogComparer(string sortExpression, string sortDirection)
        {
            this.SortExpression = sortExpression;
            this.SortDirection = sortDirection;
        }

        int IComparer<EventLogInfo>.Compare(EventLogInfo x, EventLogInfo y)
        {
            EventLogInfo oX = (SortDirection == "ASC") ? x : y;
            EventLogInfo oY = (SortDirection == "ASC") ? y : x;

            switch (SortExpression)
            {				
				case "EventLogId":
					return oX.EventLogId.CompareTo(oY.EventLogId);				
				case "PhoneId":
					return oX.PhoneId.CompareTo(oY.PhoneId);				
				case "UserId":
					return oX.UserId.CompareTo(oY.UserId);				
				case "MacAddress":
					return oX.MacAddress.CompareTo(oY.MacAddress);				
				case "IpAddress":
					return oX.IpAddress.CompareTo(oY.IpAddress);				
				case "UserAgent":
					return oX.UserAgent.CompareTo(oY.UserAgent);				
				case "EventSource":
					return oX.EventSource.CompareTo(oY.EventSource);				
				case "EventMessage":
					return oX.EventMessage.CompareTo(oY.EventMessage);				
				case "EventType":
					return oX.EventType.CompareTo(oY.EventType);				
				case "EventDate":
					return oX.EventDate.CompareTo(oY.EventDate);
				default:
                    return 0;
            }
        }
    }
}
